<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Core
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-06-01
 ***********************************************************************/
if(defined("IVANENGINE_CORE_STL_NAMESPACEARRAY"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_STL_NAMESPACEARRAY", true);

require_once (dirname(__FILE__).'/../Base.php');

class CNamespaceArray extends CBase implements Serializable
{
    const DEFAULT_NAMESPACE = 'DEFAULT::NAMESPACE';
    protected $m_array      = array();
    
    public function __construct() 
    {
        parent::__construct();
    }

    public function __destruct()
    {
        unset($this->m_array);
        parent::__destruct();
    }
    
    public function Add($array, $namespace = null)
    {
        if ($array === null) return;

        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        if (!isset($this->m_array[$namespace]))
        {
            $this->m_array[$namespace] = array();
        }

        foreach ($array as $key => $value)
        {
            $this->m_array[$namespace][$key] = $value;
        }
    }
  
    public function Set($key, $value, $namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        if (!isset($this->m_array[$namespace]))
        {
            $this->m_array[$namespace] = array();
        }

        $this->m_array[$namespace][$key] = $value;
    }
    
    public function & Get($key, $default = null, $namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        if (isset($this->m_array[$namespace][$key]))
        {
            $value = & $this->m_array[$namespace][$key];
        }
        else if (isset($this->m_array[$namespace]))
        {
            $value = CHelper::GetArrayValueForPath($this->m_array[$namespace], $key, $default);
        }
        else
        {
          $value = $default;
        }

        return $value;
    }
    
    public function GetKeys($namespace = null)
    {
        if (!$namespace)
        {
          $namespace = self::DEFAULT_NAMESPACE;
        }

        if (isset($this->m_array[$namespace]))
        {
          return array_keys($this->m_array[$namespace]);
        }

        return array();
    }
    
    public function GetNamespaces()
    {
        return array_keys($this->m_array);
    }
    
    public function & GetValues($namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        $result = array();

        if (isset($this->m_array[$namespace]))
        {
            $result = $this->m_array[$namespace];
        }

        return $result;
    }
    
    public function Has($key, $namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        if (isset($this->m_array[$namespace][$key]))
        {
            return true;
        }
        else if (isset($this->m_array[$namespace]))
        {
            return CHelper::HasArrayValueForPath($this->parameters[$namespace], $key);
        }

        return false;
    }

    public function HasNamespace($namespace)
    {
        return isset($this->m_array[$namespace]);
    }
    
    public function Remove($key, $default = null, $namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        $retval = $default;

        if (isset($this->m_array[$namespace]) && array_key_exists($key, $this->m_array[$namespace]))
        {
            $retval = $this->m_array[$namespace][$key];
            unset($this->m_array[$namespace][$key]);
        }
        else
        {
            $retval = CHelper::RemoveArrayValueForPath($this->m_array[$namespace], $key, $default);
        }

        return $retval;
    }
  
    public function & RemoveNamespace($namespace = null)
    {
        if (!$namespace)
        {
            $namespace = self::DEFAULT_NAMESPACE;
        }

        $retval = null;

        if (isset($this->m_array[$namespace]))
        {
            $retval =& $this->m_array[$namespace];
            unset($this->m_array[$namespace]);
        }

        return $retval;
    }

    public function Clear()
    {
        $this->m_array = null;
        $this->m_array = array();
    }
    
    public function serialize()
    {
        return serialize(array($this->m_array));
    }

    public function unserialize($serialized)
    {
        $this->m_array = unserialize($serialized);
    }
}
?>
